package com.aforce.behaviors;

import java.util.Enumeration;

import javax.media.j3d.Behavior;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.WakeupOnElapsedFrames;
import javax.media.j3d.WakeupOnTransformChange;
import javax.vecmath.Vector3d;

import com.aforce.entities.Ship;
import com.sun.j3d.utils.universe.ViewingPlatform;

public class EyeBehavior extends Behavior
{
	TransformGroup eyeTransformGroup = null;
	Transform3D eyeTransform = new Transform3D();
	final Ship ship;

	final static Vector3d EYE_OFFSET = new Vector3d(0.0, 0.0, 0.0);

	public EyeBehavior(ViewingPlatform view, Ship ship)
	{
		super();
		this.eyeTransformGroup = view.getViewPlatformTransform();
		this.ship = ship;
	}

	@Override
	public void initialize()
	{
		this.wakeupOn(new WakeupOnElapsedFrames(2));
	}

	@Override
	public void processStimulus(Enumeration arg0)
	{
			this.wakeupOn(new WakeupOnTransformChange(ship.getTransformGroup()));

			Transform3D currentTransform = new Transform3D();
			ship.getTransformGroup().getTransform(currentTransform);

			Vector3d eyePosition = new Vector3d(ship.getForwardVector());
			eyePosition.normalize();
			eyePosition.scale(2);

			// place eye in front of plane center
			eyePosition.add(ship.getPosition());

			currentTransform.normalize();
			currentTransform.setTranslation(eyePosition);
			eyeTransformGroup.setTransform(currentTransform);
	}
}
